package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.home.authorization;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.services.UserService;
import br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.util.Randomizer;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.InfoManager;

import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;

/**
 * @author Renato Miceli
 */
public class AuthorizationListener extends ButtonListenerAdapter {

	private UserInfo info;
	private final Randomizer randomizer;
	private final InfoManager<UserInfo> manager;

	public AuthorizationListener(Randomizer randomizer, InfoManager<UserInfo> infoManager) {
		this.randomizer = randomizer;
		this.manager = infoManager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void onClick(Button button, EventObject e) {
		MessageBox.confirm("Confirm", "Are you sure you want to authorize user \'" + this.info.getLogin() + "\'? ", new MessageBox.ConfirmCallback() {
			public void execute(String btnID) {
				if (btnID.equals("yes")) {
					UserInfo loggedUserInfo = manager.getInfo();
					UserService.Util.getInstance().authorizeUser(loggedUserInfo.getLogin(), loggedUserInfo.getPassword(), info.getLogin(),
							new AuthorizationCallback(randomizer));
				}
			}
		});
	}

	public void setUserInfo(UserInfo info) {
		this.info = info;
	}

}
